const express = require('express');
const app = express();
const port = 3000;

const Mock = require('mockjs'); //引入mock

app.get('/api/user', (req, res) => {
  console.log('请求了 /api/user 接口');

  const mockData = Mock.mock({
    code: 0,
    message: 'success',
    data: {
      name: Mock.Random.cname(2, 3),
      age: Mock.Random.integer(20, 40),
      email: Mock.Random.email(),
    },
  });

  res.json(mockData);
});
app.get('/api/error', (req, res) => {
  console.log('请求了 /api/error 接口');

  const userData = {
    code: 10,
    data: {
      id: 2,
      name: '李四',
      age: 30,
      email: 'lisi@example.com',
    },
  };

  res.json(userData);
});

app.listen(port, () => {
  console.log(`服务已经启动 => http://localhost:${port}`); // 模板字符串的反引号
});
